package org.vectrics.recruiter.reporting;

import java.awt.Color;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import org.apache.log4j.Logger;
import org.vectrics.common.reporting.pdf.ReportUtil;
import org.vectrics.domain.Domain;
import org.vectrics.provider.CoreServiceLocator;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;


public class JobStatisticSummaryPdfTable extends VectricsPdfTable {
	private Logger log = Logger.getLogger(this.getClass());
	private int startYear;
	private int prevYearCount;
	private TimeQuery timeQuery;
	private String tableHeaderPhraseCode = null;
	
	public JobStatisticSummaryPdfTable(Domain domain, Locale locale, 
			int startYear, int prevYearCount, TimeQuery timeQuery, String tableHeaderPhraseCode) {
		super(domain, locale);
		this.tableHeaderPhraseCode = tableHeaderPhraseCode;
		this.startYear = startYear;
		this.prevYearCount = prevYearCount;
		this.timeQuery = timeQuery;
	}
	
	
	protected int getColumnCount() {
		return 1 + prevYearCount + 1;
	}

	protected int getColumnWidth(int columnNumber) {
		return 200;
	}
	
	protected void addTableHeaderCell(String title) {
		log.debug("addTableHeaderCell()");
		PdfPCell hdrCell = new PdfPCell(new Phrase(title, ReportConstants.stdBoldFont));
		hdrCell.setBackgroundColor(ReportConstants.TABLE_HEAD_COLOR);
		hdrCell.setColspan(this.getColumnCount());
		this.addCell(hdrCell);	
	}
	
	protected void doFill(PdfPTable table) {
		table.setHeaderRows(2);
		this.addTableHeaderCell(this.getPhrase(this.tableHeaderPhraseCode));
		SimpleDateFormat monthFormat = CoreServiceLocator.getDateFormat()
			.getDateFormatForMonthName();
		createTableHeaderRow();
		for (int month = 0; month <= 11; month++) {
			Calendar cal = Calendar.getInstance();
			cal.setTimeInMillis(0);
			cal.set(Calendar.DATE, 1);
			cal.set(Calendar.YEAR, this.startYear);
			cal.set(Calendar.MONTH, month);
			Date date = cal.getTime();
			createRow(monthFormat.format(date), month);
		}
		this.createYearTotalRow("Year Total:");
	}

	
	protected void createTableHeaderRow() {
		Color color = new Color(255, 255, 255);
		
		log.debug("createRow() - called");
		PdfPCell cell = new PdfPCell(ReportUtil.createPhrase(" ", ReportConstants.stdFont));
		cell.setBackgroundColor(color);
		this.addCell(cell);	
	
		for (int i = 0; i < (this.prevYearCount + 1); i++) {
			int year = this.startYear - i;
			cell = new PdfPCell(ReportUtil.createPhrase("" + year, ReportConstants.stdBoldFont));
			cell.setBackgroundColor(color);
			this.addCell(cell);
		}
	}
	
	
	
	/**
	 * 
	 * @param phraseCode
	 * @param values
	 */
	protected void createRow(String rowLabel, int month) {
		Color color = new Color(255, 255, 255);
		
		log.debug("createRow() - called");
		PdfPCell cell = new PdfPCell(ReportUtil.createPhrase(
				rowLabel, ReportConstants.stdFont));
		cell.setBackgroundColor(color);
		this.addCell(cell);	
	
		for (int year = this.startYear; year >= (this.startYear - this.prevYearCount); year--) {
			int count = this.timeQuery.getMonthCount(year, month);
			cell = new PdfPCell(ReportUtil.createPhrase("" + count, ReportConstants.stdFont));
			cell.setBackgroundColor(color);
			this.addCell(cell);
		}
	}
	/**
	 * 
	 * @param phraseCode
	 * @param values
	 */
	protected void createYearTotalRow(String label) {
		Color color = new Color(255, 255, 255);
		
		log.debug("createRow() - called");
		PdfPCell cell = new PdfPCell(ReportUtil.createPhrase(
				label, ReportConstants.stdBoldFont));
		cell.setBackgroundColor(color);
		this.addCell(cell);	
	
		for (int year = this.startYear; year >= (this.startYear - this.prevYearCount); year--) {
			int count = this.timeQuery.getYearCount(year);
			cell = new PdfPCell(ReportUtil.createPhrase("" + count, ReportConstants.stdBoldFont));
			cell.setBackgroundColor(color);
			this.addCell(cell);
		}
	}


}
